package cn.itcast.goods.admin.order.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.goods.order.domain.Order;
import cn.itcast.goods.order.service.OrderService;
import cn.itcast.goods.pager.PageBean;
import cn.mytools.servlet.BaseServlet;

public class AdminOrderServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7650809017202391068L;
	private OrderService orderService = new OrderService();

	/**
	 * 获取当前页的页码
	 * 
	 * @param req
	 * @return
	 */

	private int getPc(HttpServletRequest req) {
		int pc = 1;
		String param = req.getParameter("pc");
		if (param != null && !param.trim().isEmpty()) {
			try {
				pc = Integer.parseInt(param);
			} catch (RuntimeException e) {
			} // 如果param为abc强转为int会出异常
		}
		return pc;
	}

	/**
	 * 截取url，页面中的分页导航需要使用它做为超链接的目标
	 * 
	 * @param req
	 * @return
	 */
	private String getUrl(HttpServletRequest req) {
		// 等于 /项目名/Servlet路径?method=find&pc=3
		String url = req.getRequestURI() + "?" + req.getQueryString();
		int index = url.lastIndexOf("&pc=");
		if (index != -1) {
			url = url.substring(0, index);
		}
		return url;
	}

	/**
	 * 查询所有订单
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);
		PageBean<Order> pb = orderService.findAll(pc);

		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "f:/adminjsps/admin/order/list.jsp";
	}

	public String findByStatus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int pc = getPc(req); // 得到pagecode当前页码
		String url = getUrl(req);
		int status = Integer.parseInt(req.getParameter("status"));
		PageBean<Order> pb = orderService.findByStatus(status, pc);

		pb.setUrl(url);
		req.setAttribute("pb", pb);
		return "f:/adminjsps/admin/order/list.jsp";
	}

	public String findByOid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String oid = req.getParameter("oid");
		Order order = orderService.findByOid(oid);
		req.setAttribute("order", order);
		return "f:/adminjsps/admin/order/desc.jsp";
	}

	/**
	 * 取消订单
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String cancel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String oid = req.getParameter("oid");
		int status = orderService.findStatus(oid);
		if (status == 1) {
			orderService.updateStatus(oid, 5);
			req.setAttribute("msg", "订单已取消！");
			req.setAttribute("code", "success");
			return "f:/adminjsps/msg.jsp";
		} else {
			req.setAttribute("msg", "状态异常，取消订单失败！");
			req.setAttribute("code", "error");
			return "f:/adminjsps/msg.jsp";
		}
	}

	/**
	 * 发货
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String send(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String oid = req.getParameter("oid");
		int status = orderService.findStatus(oid);
		if (status == 2) {
			orderService.updateStatus(oid, 3);
			req.setAttribute("msg", "发货成功！");
			req.setAttribute("code", "success");
			return "f:/adminjsps/msg.jsp";
		} else {
			req.setAttribute("msg", "状态异常，发货失败！");
			req.setAttribute("code", "error");
			return "f:/adminjsps/msg.jsp";
		}
	}
}
